<script setup lang="ts">
import {defineExpose, defineProps, reactive, ref} from "vue";
import {
  AddText,
  GetSecretKeyList,
} from "../../../../wailsjs/go/view/SecretText";
import {base, tables} from "../../../../wailsjs/go/models.ts";

const props = defineProps({
  onLoadSecretTextList: {
    type: Function,
    required: true
  }
})

const dialogVisible = ref(false)
const addForm = reactive<base.AddSecretText>({
  title: "",
  label: "",
  textType: "simple",
  content: "",
  keyId: "1",
  adminKey: "",
  prompt: "",
  isPrivate: false,
})
const keyList = ref<{}[]>([])

const clickSubBtn = ()=>{
  AddText(addForm).then(()=>{
    dialogVisible.value = false
    props.onLoadSecretTextList()
  })
}
const onDialogOpened = ()=>{
  GetSecretKeyList().then(res =>{
    keyList.value = []
    res.forEach(item=>{
      keyList.value.push({
        value: item.id.toString(),
        label: item.title,
      })
    })
  })
}

defineExpose({
  dialogVisible,
  addForm
})
</script>

<template>
  <el-dialog v-model="dialogVisible" @open="onDialogOpened">
    <el-form :model="addForm" label-width="auto" >
      <el-form-item label="标题">
        <el-input v-model="addForm.title" />
      </el-form-item>
      <el-form-item label="标签">
        <el-input v-model="addForm.label" />
      </el-form-item>
      <el-form-item label="密钥">
        <el-select
          v-model="addForm.keyId"
          :options="keyList"
          placeholder="选择密钥"
        />
      </el-form-item>
      <el-form-item label="管理密码">
        <el-input v-model="addForm.adminKey" show-password/>
      </el-form-item>
      <el-form-item label="密码提示">
        <el-input v-model="addForm.prompt" />
      </el-form-item>
      <el-form-item label="类型">
        <el-radio-group v-model="addForm.textType">
          <el-radio value="simple">简易文本</el-radio>
          <el-radio value="chapter">章节</el-radio>
          <el-radio value="doc">复合文本</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="内容" >
        <el-input v-model="addForm.content" type="textarea" :rows="5"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible =false">取消</el-button>
        <el-button type="primary" @click="clickSubBtn">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
